/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Competente;

import adsreis.com.Beans.ClientesBean;
import adsreis.com.Controls.ClientesControl;
import adsreis.com.Outros.RetirarMascaras;
import adsreis.com.acessobd.VerificaAcesso;
import java.awt.AWTKeyStroke;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashSet;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;



/**
 *
 * @author Pauroenriki
 */
public class Clientes extends javax.swing.JFrame {
    DefaultTableModel tmClientes = new DefaultTableModel(null, new String[]{"Codigo","Nome", "CPF", "Fone", "Celular", "Operadora", "E-mail"});
    ListSelectionModel lsmClientes;
    List<ClientesBean> clientes;
    String tipoCadastro;
    String User;
    Integer codcli;
    Integer Acesso;
    
    /**
     * Creates new form Clientes
     */
    public Clientes() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
        initComponents();
        setIcon();
        Login ff = new Login();
        Acesso = ff.nAcesso;
        User = ff.acessivel;
        Nacesso();
        this.setTitle("CADASTRAR CLIENTES - Usuário logado: " + User);
        HashSet conj = new HashSet(this.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS)); 
        conj.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_ENTER, 0));
        conj.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_ENTER, 0));
        this.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, conj); 
        
        
    }

    
    private void Nacesso(){
        
        switch (Acesso){
            case 0:
                
                btExcluir.setVisible(false);
                btAlterar.setVisible(false);
            case 1:
                btExcluir.setVisible(false);
                btAlterar.setVisible(false);
            case 2:
            
        }
        
        
    }
  
    
    protected void cancelarCadastro(){
        while (tmClientes.getRowCount()>0){
            tmClientes.removeRow(0);            
        }
        desabilitarCampos();
        btSalvar.setEnabled(false);
        btAlterar.setEnabled(false);
        btExcluir.setEnabled(false);
        
    }
    
    
    private void setIcon() {
        //etIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("a.png")));﻿
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Icons/icon.png")));
    }
    
    protected void alteraFuncionario(){
        if(tbClientes.getSelectedRow() != -1){
            habilitarCampos();
            
        } else {
            JOptionPane.showInternalMessageDialog(this, "Selecione um Cliente");
        }
    }
    
    protected void alterarCliente(){
        SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
        
        if(verificarCampos()){
           habilitarCampos();           
           ClientesBean cliente = new ClientesBean();
           cliente.setCodCliente(clientes.get(tbClientes.getSelectedRow()).getCodCliente());
           cliente.setNome(tfNome.getText().trim());
           String cpf;
            RetirarMascaras rm = new RetirarMascaras();
            cpf = rm.RetirarMascaraCPF(tfCpf.getText().trim());
            cliente.setCpf(cpf);
            String rg;
            rg = rm.RetirarMascaraRG(tfRg.getText().trim());
            cliente.setRg(rg);
            String fone;
            fone = rm.RetirarMascaraTELEFONE(ftfTelefone.getText().trim());
            cliente.setTelefone(fone);
            String cel;
            cel = rm.RetirarMascaraTELEFONE(ftfCelular.getText().trim());
            cliente.setCelular(cel);
           cliente.setEndereco(tfEndereco.getText().trim());
           cliente.setBairro(tfBairro.getText().trim());
           cliente.setCidade(tfCidade.getText().trim());
           cliente.setEstado(tfEstado.getText().trim());
           cliente.setOperadora(tfOperadora.getText().trim());
           cliente.setEmail(tfEmail.getText().trim());
           ClientesControl c = new ClientesControl();
           c.alterarCliente(cliente);
           
           
           
           
           desabilitarCampos();
           
       }else{
           JOptionPane.showMessageDialog(this, "Informe o nome do Cliente");
           tfNome.requestFocus();
       }
   }
    
    protected void novoCliente(){
        habilitarCampos();
        ClientesControl cc = new ClientesControl();
        codcli = cc.buscarCodigoUltimoCliente()+ 1;
        lblCodigo.setText(String.valueOf(codcli));
       tfNome.requestFocus();
        
            tfNome.setText("");
            tfEndereco.setText("");
            tfBairro.setText("");
            tfCidade.setText("");
            tfEstado.setText("");
            tfCpf.setText("");
            tfRg.setText("");
            ftfTelefone.setValue(null);
            ftfCelular.setValue(null);
            tfOperadora.setText("");
            tfEmail.setText("");
        
        habilitarCampos();
        ftfTelefone.setText("");
        ftfCelular.setText("");
        btSalvar.setEnabled(true);
        
        while (tmClientes.getRowCount()>0){
            tmClientes.removeRow(0);            
        }
        
    }
    
    
    
    protected boolean verificarCampos(){
        if(!tfNome.getText().trim().equals("")){
            
            return true;
        }
        JOptionPane.showMessageDialog(this, "É necessário inserir todos dados do cliente para efetuar o cadastro!");
        return false;
    }
    
    protected void listarClientes(){
        ClientesControl func = new ClientesControl();
        clientes = func.listarClientes("%" + tfPesquisaClientes.getText().trim() + "%");
        
        mostrarClientes(clientes);
    }
    
    
    protected void habilitarCampos(){
        
        tfNome.setEnabled(true);
        tfEndereco.setEnabled(true);
        tfBairro.setEnabled(true);
        tfCidade.setEnabled(true);
        tfEstado.setEnabled(true);
        tfCpf.setEnabled(true);
        tfRg.setEnabled(true);
        ftfTelefone.setEnabled(true);
        ftfCelular.setEnabled(true);
        tfOperadora.setEnabled(true);
        tfEmail.setEnabled(true);
        
        
        
        
        
    }
    
    protected void desabilitarCampos(){
        
        tfNome.setEnabled(false);
        tfEndereco.setEnabled(false);
        tfBairro.setEnabled(false);
        tfCidade.setEnabled(false);
        tfEstado.setEnabled(false);
        tfCpf.setEnabled(false);
        tfRg.setEnabled(false);
        ftfTelefone.setEnabled(false);
        ftfCelular.setEnabled(false);
        tfOperadora.setEnabled(false);
        tfEmail.setEnabled(false);
        
        
        lblCodigo.setText("(Novo)");
            tfNome.setText("");
            tfEndereco.setText("");
            tfBairro.setText("");
            tfCidade.setText("");
            tfEstado.setText("");
            tfCpf.setText("");
            tfRg.setText("");
            ftfTelefone.setValue("");
            ftfCelular.setValue("");
            tfOperadora.setText("");
            tfEmail.setText("");
        
        
            
            
            
            
    }
    
    protected boolean cadastraCliente(){
        
        
                
            
       try{ 
        
        if (verificarCampos()){
            SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
            ClientesBean cliente = new ClientesBean();
            cliente.setNome(tfNome.getText().trim());
            String cpf;
            RetirarMascaras rm = new RetirarMascaras();
            cpf = rm.RetirarMascaraCPF(tfCpf.getText().trim());
            cliente.setCpf(cpf);
            String rg;
            rg = rm.RetirarMascaraRG(tfRg.getText().trim());
            cliente.setRg(rg);
            String fone;
            fone = rm.RetirarMascaraTELEFONE(ftfTelefone.getText().trim());
            cliente.setTelefone(fone);
            String cel;
            cel = rm.RetirarMascaraTELEFONE(ftfCelular.getText().trim());
            cliente.setCelular(cel);
            cliente.setCodCliente(codcli);
            cliente.setEndereco(tfEndereco.getText().trim());
            cliente.setBairro(tfBairro.getText().trim());
            cliente.setCidade(tfCidade.getText().trim());
            cliente.setEstado(tfEstado.getText().trim());
            
            cliente.setOperadora(tfOperadora.getText().trim());
            cliente.setEmail(tfEmail.getText().trim());
            
            ClientesControl c = new ClientesControl();
            c.cadastrarCliente(cliente);
            JOptionPane.showMessageDialog(this, "Cliente cadastrado com sucesso!");
            return true;
        }
       }catch (Exception e){
            e.getMessage();
            JOptionPane.showMessageDialog(this, "Preencha o Nome do Cliente");
            tfNome.requestFocus();
            return false;
        }
        return false;
       
    }
    
    
     protected void mostrarClientes(List<ClientesBean> clientes){
        while(tmClientes.getRowCount()>0){
            tmClientes.removeRow(0);
        }
        if (clientes.size() == 0){
            JOptionPane.showMessageDialog(this,"Nenhum cliente encontrado!");
        } else{
            String[] campos = new String[]{null, null, null, null, null, null, null};
            for(int i=0; i< clientes.size(); i++){
                tmClientes.addRow(campos);
                tmClientes.setValueAt(clientes.get(i).getCodCliente(), i, 0);
                tmClientes.setValueAt(clientes.get(i).getNome(), i, 1);
                tmClientes.setValueAt(clientes.get(i).getCpf(), i, 2);
                tmClientes.setValueAt(clientes.get(i).getTelefone(), i, 3);
                tmClientes.setValueAt(clientes.get(i).getCelular(), i, 4);
                tmClientes.setValueAt(clientes.get(i).getOperadora(), i, 5);
                tmClientes.setValueAt(clientes.get(i).getEmail(), i, 6);
                
                
            }
        }
    }
    
    protected void excluirCliente(){
        
        
        
        if (tbClientes.getSelectedRow() != -1 ){
            int resposta = JOptionPane.showConfirmDialog(this, "Tem certeza que quer excluir o funcionario selecionado?", "Confirmação",
                    JOptionPane.YES_NO_OPTION);
            
            if (resposta == JOptionPane.YES_OPTION){
                
                
                
                ClientesControl pc = new ClientesControl();
                pc.excluirCliente(clientes.get(tbClientes.getSelectedRow()).getCodCliente());
               
                
                listarClientes();
                btAlterar.setEnabled(false);
                btExcluir.setEnabled(false);
                
                
                
            }
        } 
    }
    
    
    protected void tbClientesLinhaSelecionada(JTable tb){
        if (tb.getSelectedRow() != -1){
            btAlterar.setEnabled(true);
            btExcluir.setEnabled(true);
            lblCodigo.setText(String.valueOf(clientes.get(tb.getSelectedRow()).getCodCliente()));
            tfNome.setText(clientes.get(tb.getSelectedRow()).getNome());
            tfEndereco.setText(clientes.get(tb.getSelectedRow()).getEndereco());
            tfBairro.setText(clientes.get(tb.getSelectedRow()).getBairro());
            tfCidade.setText(clientes.get(tb.getSelectedRow()).getCidade());
            tfEstado.setText(clientes.get(tb.getSelectedRow()).getEstado());
            tfCpf.setText(clientes.get(tb.getSelectedRow()).getCpf());
            tfRg.setText(clientes.get(tb.getSelectedRow()).getRg());         
            ftfTelefone.setText(clientes.get(tb.getSelectedRow()).getTelefone());
            ftfCelular.setText(clientes.get(tb.getSelectedRow()).getCelular());
            tfOperadora.setText(clientes.get(tb.getSelectedRow()).getOperadora());
            tfEmail.setText(clientes.get(tb.getSelectedRow()).getEmail());
        } else {
            desabilitarCampos();
        }
    }
    private void fechar(){
        
        tbClientes.removeAll();
        if (clientes != null){
while (clientes.size()>0){
            clientes.remove(0);           
        }
        }
        
while (tmClientes.getRowCount()>0){
            tmClientes.removeRow(0);            
        }

    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfNome = new javax.swing.JTextField();
        tfEndereco = new javax.swing.JTextField();
        tfBairro = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tfCidade = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tfEstado = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        ftfCelular = new javax.swing.JFormattedTextField();
        ftfTelefone = new javax.swing.JFormattedTextField();
        lblCodigo = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        tfCpf = new javax.swing.JFormattedTextField();
        tfRg = new javax.swing.JFormattedTextField();
        jPanel1 = new javax.swing.JPanel();
        btExcluir = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbClientes = new javax.swing.JTable();
        btAlterar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        tfPesquisaClientes = new javax.swing.JTextField();
        btSalvar = new javax.swing.JButton();
        btPesquisar = new javax.swing.JButton();
        btNovo = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        tfOperadora = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        tfEmail = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("CADASTRO DE FUNCIONARIOS");
        setMinimumSize(new java.awt.Dimension(805, 519));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                Sair(evt);
            }
        });

        jLabel1.setText("Nome");

        tfNome.setEnabled(false);

        tfEndereco.setEnabled(false);

        tfBairro.setEnabled(false);

        jLabel2.setText("Endereço");

        jLabel3.setText("Bairro");

        jLabel4.setText("Cidade");

        tfCidade.setEnabled(false);

        jLabel5.setText("Estado");

        tfEstado.setEnabled(false);

        jLabel6.setText("CPF");

        jLabel7.setText("RG");

        jLabel8.setText("Fone");

        jLabel9.setText("Celular");

        try {
            ftfCelular.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##) #####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        ftfCelular.setEnabled(false);
        ftfCelular.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                celular(evt);
            }
        });

        try {
            ftfTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        ftfTelefone.setEnabled(false);

        lblCodigo.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblCodigo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCodigo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel10.setText("Codigo");

        try {
            tfCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        tfCpf.setToolTipText("");
        tfCpf.setEnabled(false);

        try {
            tfRg.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###/A")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        tfRg.setEnabled(false);

        btExcluir.setText("Excluir");
        btExcluir.setEnabled(false);
        btExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcluirActionPerformed(evt);
            }
        });

        tbClientes.setModel(tmClientes);
        tbClientes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        lsmClientes = tbClientes.getSelectionModel();
        lsmClientes.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e){
                if(! e.getValueIsAdjusting()){
                    tbClientesLinhaSelecionada(tbClientes);
                }
            }
        });
        jScrollPane1.setViewportView(tbClientes);

        btAlterar.setText("Alterar");
        btAlterar.setEnabled(false);
        btAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAlterarActionPerformed(evt);
            }
        });

        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        btSalvar.setText("Salvar");
        btSalvar.setEnabled(false);
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        btPesquisar.setText("Pesquisar");
        btPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarActionPerformed(evt);
            }
        });

        btNovo.setText("Novo");
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(tfPesquisaClientes, javax.swing.GroupLayout.PREFERRED_SIZE, 663, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btNovo)
                        .addGap(73, 73, 73)
                        .addComponent(btSalvar)
                        .addGap(63, 63, 63)
                        .addComponent(btAlterar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btExcluir)
                        .addGap(70, 70, 70)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 779, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btAlterar, btCancelar, btExcluir, btNovo, btPesquisar, btSalvar});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btNovo)
                        .addComponent(btSalvar)
                        .addComponent(btAlterar))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btExcluir)
                        .addComponent(btCancelar)))
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfPesquisaClientes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btPesquisar))
                .addGap(11, 11, 11)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jLabel11.setText("Operadora");

        tfOperadora.setEnabled(false);

        jLabel20.setText("E-mail");

        tfEmail.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(290, 290, 290)
                                        .addComponent(jLabel10)
                                        .addGap(10, 10, 10)
                                        .addComponent(lblCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addGap(22, 22, 22)
                                        .addComponent(tfNome, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)))
                                .addGap(294, 294, 294))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(tfOperadora, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel7)
                                    .addGap(35, 35, 35)
                                    .addComponent(tfRg, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jLabel8)
                                    .addGap(18, 18, 18)
                                    .addComponent(ftfTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(jLabel9)
                                    .addGap(18, 18, 18)
                                    .addComponent(ftfCelular, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jLabel11))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel3))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(tfBairro)
                                            .addGap(10, 10, 10)
                                            .addComponent(jLabel4)
                                            .addGap(10, 10, 10)
                                            .addComponent(tfCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(16, 16, 16)
                                            .addComponent(jLabel5)
                                            .addGap(31, 31, 31)
                                            .addComponent(tfEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(18, 18, 18)
                                            .addComponent(jLabel6)
                                            .addGap(18, 18, 18)
                                            .addComponent(tfCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(tfEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, 731, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 731, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(10, 10, 10)
                                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 780, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(jLabel20))))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(lblCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel1))
                    .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel2))
                    .addComponent(tfEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel6)
                                .addComponent(tfCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfRg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftfTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(ftfCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel11)
                        .addComponent(tfOperadora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20)
                    .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        getAccessibleContext().setAccessibleDescription("");

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        tipoCadastro = "novo";
        novoCliente();
        
    }//GEN-LAST:event_btNovoActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
   
         if (tipoCadastro.equals("novo")){
            //cadastraCliente();
             if (cadastraCliente()){
            desabilitarCampos();
            btSalvar.setEnabled(false);
            btAlterar.setEnabled(false);
            btExcluir.setEnabled(false);
         }else{
                 tfNome.requestFocus();
             }
        }else if (tipoCadastro.equals("alteracao")){
            alterarCliente();
            btSalvar.setEnabled(false);
            btAlterar.setEnabled(false);
            btExcluir.setEnabled(false);
            listarClientes();
        
    }
    }//GEN-LAST:event_btSalvarActionPerformed

    private void btAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAlterarActionPerformed
        tipoCadastro = "alteracao";
        habilitarCampos();
        btSalvar.setEnabled(true);
        
    }//GEN-LAST:event_btAlterarActionPerformed

    private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcluirActionPerformed
        excluirCliente();
    }//GEN-LAST:event_btExcluirActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        cancelarCadastro();
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarActionPerformed
        listarClientes();
    }//GEN-LAST:event_btPesquisarActionPerformed

    private void celular(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_celular
      
        
         
        
    }//GEN-LAST:event_celular

    private void Sair(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_Sair
      tbClientes.getSelectionModel().clearSelection();
        fechar();
    }//GEN-LAST:event_Sair

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Clientes().setVisible(true);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(Clientes.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(Clientes.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(Clientes.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(Clientes.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton btAlterar;
    public javax.swing.JButton btCancelar;
    public javax.swing.JButton btExcluir;
    public javax.swing.JButton btNovo;
    public javax.swing.JButton btPesquisar;
    public javax.swing.JButton btSalvar;
    public javax.swing.JFormattedTextField ftfCelular;
    public javax.swing.JFormattedTextField ftfTelefone;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JLabel lblCodigo;
    public javax.swing.JTable tbClientes;
    public javax.swing.JTextField tfBairro;
    public javax.swing.JTextField tfCidade;
    public javax.swing.JFormattedTextField tfCpf;
    private javax.swing.JTextField tfEmail;
    public javax.swing.JTextField tfEndereco;
    public javax.swing.JTextField tfEstado;
    public javax.swing.JTextField tfNome;
    private javax.swing.JTextField tfOperadora;
    public javax.swing.JTextField tfPesquisaClientes;
    public javax.swing.JFormattedTextField tfRg;
    // End of variables declaration//GEN-END:variables

    private VerificaAcesso VerificaAcesso() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
